package cn.jiangjiesheng.bootstrap.fileport.domain.doc;

import cn.jiangjiesheng.bootstrap.fileport.domain.doc.model.ReportDataModel;
import cn.jiangjiesheng.bootstrap.fileport.infrastructure.utils.FileUtils;
import cn.jiangjiesheng.bootstrap.fileport.infrastructure.utils.JsonUtils;
import lombok.var;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by dev@jiangjiesheng.cn on 2021/3/22  <br/>
 */
public class ReportGenService {

    // 在Spring Boot中可以使用Autoware引入
    private ReportToolService reportToolService = new ReportToolService();

    void genReport() throws IOException {
        var tpl = FileUtils.readInputStreamFromResource("templates/templ.docx");
        var data = new ReportDataModel()
                .setEChart("image", JsonUtils.toJsonNode(FileUtils.readStringFromResource("templates/testdata/pie.json")),400,300)
                .setText("title", "安元")
                .setText("name","名称")
                .setTable("table", new String[][]{
                        new String[]{"名称", "年龄"},
                        new String[]{"小张", "11"},
                        new String[]{"小米", "23"},
                        new String[]{"小红", "98"},
                });
        InputStream file = reportToolService.genReport(tpl, data);
        byte[] pdfOut = reportToolService.convertToPdf(file);
        OutputStream file1 = new FileOutputStream("testout1.pdf");
        file1.write(pdfOut);
        file1.close();
    }
}
